//
//  HNSH_CartShopHeader.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/3/2.
//

#import "HNSH_CartShopHeader.h"

@interface HNSH_CartShopHeader ()

/* bgView */
@property (nonatomic, strong) UIView *bgView;
/* selectButton */
@property (nonatomic, strong) UIButton *selectButton;
/* shopIcon */
@property (nonatomic, strong) UIImageView *shopIcon;
/* showIcon */
@property (nonatomic, strong) UIImageView *showIcon;
/* shopLabel */
@property (nonatomic, strong) UILabel *shopLabel;

/* lineView */
@property (nonatomic, strong) UIView *lineView;

@end

@implementation HNSH_CartShopHeader

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithReuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = [UIColor appBGColor];
        [self configUI];
    }
    return self;
}

- (void)setModel:(HNSH_CartShopModel *)model {
    _model = model;
    self.shopLabel.text = model.shop_name;
    self.selectButton.selected = model.selected;
}

- (void)configUI {
    [self.contentView addSubview:self.bgView];
    
    [self.bgView addSubview:self.selectButton];
    [self.bgView addSubview:self.shopIcon];
    [self.bgView addSubview:self.shopLabel];
    [self.bgView addSubview:self.showIcon];
    
    [self.bgView addSubview:self.lineView];
    
    [self.selectButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(44);
    }];
    
    [self.shopIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.selectButton.mas_centerY);
        make.left.mas_equalTo(40);
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(20);
    }];
    [self.shopLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.selectButton.mas_centerY);
        make.left.equalTo(self.shopIcon.mas_right).offset(8);
        make.height.mas_equalTo(20);
    }];
    [self.showIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.selectButton.mas_centerY);
        make.left.equalTo(self.shopLabel.mas_right).offset(6);
        make.width.mas_equalTo(16);
        make.height.mas_equalTo(16);
    }];
    
    WeakSelf(weakSelf);
    self.shopLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(id  _Nonnull sender) {
        if (weakSelf.headerBlock) {
            weakSelf.headerBlock(weakSelf.model.shop_id,weakSelf.model.shop_name);
        }
    }];
    [self.shopLabel addGestureRecognizer:tap];
}

#pragma mark lazy
- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 52)];
        _bgView.backgroundColor = [UIColor appWhiteLabelColor];
        [self clipCornerWithView:_bgView cornerWidth:8 cornerType:UIRectCornerTopLeft|UIRectCornerTopRight];
    }
    return _bgView;
}

- (UIButton *)selectButton {
    if (!_selectButton) {
        _selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_selectButton setImage:[UIImage imageNamed:@"base_select_off"] forState:UIControlStateNormal];
        [_selectButton setImage:[UIImage imageNamed:@"base_select_on"] forState:UIControlStateSelected];
        WeakSelf(weakSelf);
        [[_selectButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            UIButton *button = x;
            button.selected = !button.selected;
            weakSelf.model.selected = button.isSelected;
            [weakSelf.model headerChangeSelectedStatus:button.isSelected];
            if (weakSelf.reloadBlock) {
                weakSelf.reloadBlock(weakSelf.model);
            }
        }];
    }
    return _selectButton;
}

- (UIImageView *)shopIcon {
    if (!_shopIcon) {
        _shopIcon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"goods_cart_shop_icon"]];
    }
    return _shopIcon;
}

- (UILabel *)shopLabel {
    if (!_shopLabel) {
        _shopLabel = [[UILabel alloc]init];
        _shopLabel.text = @"";
        _shopLabel.textColor = [UIColor appBlackLabelColor];
        _shopLabel.font = [UIFont boldSystemFontOfSize:14];
    }
    return _shopLabel;
}

- (UIImageView *)showIcon {
    if (!_showIcon) {
        _showIcon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"goods_detail_show"]];
    }
    return _showIcon;
}

- (UIView *)lineView {
    if (!_lineView) {
        _lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 44, kScreenWidth, 1)];
        _lineView.backgroundColor = [UIColor colorWithHexString:@"#E3E3E3"];
    }
    return _lineView;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
